package com.qiaofei.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiaofei.mapper.OldHouseMapper;
import com.qiaofei.pojo.OldHouse;
import com.qiaofei.pojo.PageBean;
import com.qiaofei.service.OldHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OldHouseServiceImpl extends BaseServiceImpl implements OldHouseService {

    @Autowired
    private OldHouseMapper oldHouseMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, Map<String ,Object> params, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<OldHouse> oldHouseList = oldHouseMapper.list(params, begin, end);
        Page<OldHouse> p = (Page<OldHouse>) oldHouseList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult(), p.getPages());
        return pageBean;
    }

    @Override
    public int add(OldHouse oldHouse) {
        oldHouse.setCreateTime(LocalDateTime.now());
        return oldHouseMapper.insert(oldHouse);
    }

    @Override
    public int update(OldHouse oldHouse) {
        oldHouse.setUpdateTime(LocalDateTime.now());
        return oldHouseMapper.update(oldHouse);
    }

    @Override
    public List<OldHouse> getByIds(List<Integer> ids) {
        List<OldHouse> oldHouseList = oldHouseMapper.getByIds(ids);
        return oldHouseList;
    }

    @Override
    public int delete(List<Integer> ids) {
        List<OldHouse> oldHouseList = getByIds(ids);
        List<String> headerList = new ArrayList<>();
        if(oldHouseList.size() > 0){
            for (OldHouse oldHouse :
                    oldHouseList) {
                if(oldHouse.getHouseImages().equals("") == false) {
                    headerList.add(oldHouse.getHouseImages());
                }
            }
            deleteImages(headerList);
        }
        return oldHouseMapper.delete(ids);
    }
}
